package controller;

import entity.Cat;
import entity.Choice;
import service.CatService;
import service.ChoiceService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/choice")
public class ChoiceController extends BaseServlet{
    ChoiceService choiceService=new ChoiceService();
    CatService catService=new CatService();

    public void getChoice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 从请求中获取参数
        String catId=request.getParameter("catId");
        String shopId=request.getParameter("shopId");

        // 根据宠物猫id查询宠物猫信息
        String sql2="select * from cat where id=?";
        Cat cat=catService.getCatById(sql2,catId);
        // 将宠物猫信息传输给页面
        request.setAttribute("cat",cat);

        // sql语句
        String sql="SELECT * FROM choice WHERE shopId=?";
        // 查询套餐列表
        List<Choice> choiceList=choiceService.getChoice(sql,shopId,catId);
        // 将查询到的套餐数据传输给前端
        request.setAttribute("choiceList",choiceList);
        // 跳转
        request.getRequestDispatcher("view/choice.jsp").forward(request,response);
    }
}
